package com.managementemployees.activities;

import android.app.ActionBar;
import android.app.ActionBar.Tab;
import android.app.ActionBar.TabListener;
import android.app.FragmentTransaction;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;

import com.managementemployees.adapter.TabsPagerAdapter;

public class MainActivity extends FragmentActivity implements TabListener {

	private ViewPager mViewPager;
	private TabsPagerAdapter mTabsPagerAdapter;
	private ActionBar mActionBar;
	// Tab titles
	private String[] tabs = { "Nhan Vien", "Cham Cong", "Bao Cao" };

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		init();
	}

	void init() {
		mViewPager = (ViewPager) findViewById(R.id.pager);
		mActionBar = getActionBar();
		mTabsPagerAdapter = new TabsPagerAdapter(getSupportFragmentManager());

		mViewPager.setAdapter(mTabsPagerAdapter);
		mActionBar.setHomeButtonEnabled(false);
		mActionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

		// adding tabs
		for (String tab_name : tabs) {
			mActionBar.addTab(mActionBar.newTab().setText(tab_name)
					.setTabListener(this));
		}
		mViewPager.setOnPageChangeListener(new OnPageChangeListener() {

			@Override
			public void onPageSelected(int position) {
				// TODO Auto-generated method stub
				mActionBar.setSelectedNavigationItem(position);
			}

			@Override
			public void onPageScrolled(int arg0, float arg1, int arg2) {
				// TODO Auto-generated method stub

			}

			@Override
			public void onPageScrollStateChanged(int arg0) {
				// TODO Auto-generated method stub

			}
		});

	}

	@Override
	public void onTabReselected(Tab tab, FragmentTransaction ft) {
		// TODO Auto-generated method stub

	}

	@Override
	public void onTabSelected(Tab tab, FragmentTransaction ft) {
		// TODO Auto-generated method stub
		mViewPager.setCurrentItem(tab.getPosition());
	}

	@Override
	public void onTabUnselected(Tab tab, FragmentTransaction ft) {
		// TODO Auto-generated method stub

	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// TODO Auto-generated method stub
		getMenuInflater().inflate(R.menu.menu_main, menu);
		return super.onCreateOptionsMenu(menu);
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// TODO Auto-generated method stub
		if (item.getItemId() == R.id.action_newNhanVien) {
			Intent i = new Intent(MainActivity.this, SaveNhanVienActivity.class);
			startActivity(i);

		}
		return super.onOptionsItemSelected(item);
	}
}
